package com.example.basic.utils;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Point;
import android.os.Build;
import android.view.Display;
import android.view.Gravity;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;

import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;
import androidx.annotation.NonNull;

public class SystemUIUtils {

    public static void setSystemUiState(Activity activity, @ColorRes int color) {
        int effectedColor = activity.getResources().getColor(color);
        setSystemUiStateInternal(activity, effectedColor);
    }

    private static void setSystemUiStateInternal(Activity activity, @ColorInt int effectedColor) {
        if ((activity == null) || activity.isFinishing()) {
            return;
        }

        setRootView(activity);
        Window window = activity.getWindow();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.setStatusBarColor(effectedColor);
        } else {
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

            ViewGroup decorView = (ViewGroup) window.getDecorView();

            int vis = decorView.getSystemUiVisibility();
//            if (CqttechColorUtils.isBrightColor(effectedColor)) {
//                vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
//            } else if (CqttechColorUtils.isDarkColor(effectedColor)) {
                vis &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
//            }

            vis |= View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            vis |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
            decorView.setSystemUiVisibility(vis);
        }
    }

    /**
     * 获取状态栏高度
     *
     * @param context context
     * @return 状态栏高度
     */
    public static int getStatusBarHeight(Context context) {
        // 获得状态栏高度
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        return context.getResources().getDimensionPixelSize(resourceId);
    }

    /**
     * 设置根布局参数
     */
    private static void setRootView(Activity activity) {
        View content = activity.findViewById(android.R.id.content);
        if (content instanceof ViewGroup) {
            View root = ((ViewGroup) content).getChildAt(0);
            if (root instanceof ViewGroup) {
                root.setFitsSystemWindows(true);
                ((ViewGroup) root).setClipToPadding(true);
            }
        }
    }

    /**
     * copy from ApiCompatibilityUtils, should use ApiCompatibilityUtils implementation as much as possible
     * @param activity should not null
     * @param statusBarColor expect status bar color
     */
    public static void setStatusBarColor(@NonNull Activity activity, @ColorInt int statusBarColor) {
        Window window = activity.getWindow();
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O && statusBarColor == Color.BLACK
                && window.getNavigationBarColor() == Color.BLACK) {
            window.clearFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        } else {
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        }
        window.setStatusBarColor(statusBarColor);
    }

    public static boolean isNavigationShow(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            Display display = activity.getWindowManager().getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);
            Point realSize = new Point();
            display.getRealSize(realSize);
            return realSize.y != size.y;
        } else {
            boolean menu = ViewConfiguration.get(activity).hasPermanentMenuKey();
            boolean back = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
            return !menu && !back;
        }
    }

    public static int getNavigationBarHeight(Activity activity) {
        Resources resources = activity.getResources();
        int resourceId = resources.getIdentifier(
                "navigation_bar_height",
                "dimen",
                "android");
        //获取NavigationBar的高度
        return resources.getDimensionPixelSize(resourceId);
    }
}
